import { IUserInfo } from "@/apis/types/login";
import Taro from "@tarojs/taro";
import { computed, ref } from "vue";
import { USER_INFO_KEY } from "../constants";

const userInfoCache = Taro.getStorageSync(USER_INFO_KEY);
const userInfo = ref<IUserInfo | null>(
  userInfoCache ? JSON.parse(userInfoCache) : null
);

export const useUserInfo = () => {
  const setUserInfo = (userInfoData: IUserInfo | null) => {
    userInfo.value = userInfoData;
    updateUserInfoStorage();
  };
  const setNickname = (nickname: string) => {
    if (!userInfo.value) return;

    userInfo.value.nickname = nickname;
    updateUserInfoStorage();
  };

  const setAvatar = (avatar: string) => {
    // console.log(userInfo.value);
    if (!userInfo.value) return;

    userInfo.value.avatar = avatar;

    // console.log(avatar);
    // console.log(userInfo.value.wechat.avatar);
    updateUserInfoStorage();
  };

  const setPhone = (phone: string) => {
    if (!userInfo.value) return;

    userInfo.value.phone = phone;
    updateUserInfoStorage();
  };

  const setHealthTarget = (prop: string, value: string) => {
    if (!userInfo.value) return;
    if (prop === "bloodPressure") {
      const [sbp, dbp] = value.split("/");
      userInfo.value.bloodPressureSbp = sbp;
      userInfo.value.bloodPressureDbp = dbp;
    } else {
      userInfo.value[prop] = value;
    }

    updateUserInfoStorage();
  };

  const updateUserInfoStorage = () => {
    Taro.setStorageSync(USER_INFO_KEY, JSON.stringify(userInfo.value));
  };

  const nickname = computed(() => {
    const defaultName = "微信用户";
    if (userInfo.value === null || userInfo.value === void 0)
      return defaultName;
    // console.log(userInfo.value);
    return userInfo.value.nickname || defaultName;
  });

  const avatar = computed(() => {
    const defaultUrl =
      "http://www.hicc.info:6002/fdfs/group1/M00/00/38/rBQACWfygjGAPkdPAAAr393KWRY648.png";
    if (userInfo.value === null) return defaultUrl;
    return userInfo.value.avatar || defaultUrl;
  });

  const phone = computed(() => {
    const defaultPhone = "--";
    if (userInfo.value === null) return defaultPhone;
    return userInfo.value.phone || defaultPhone;
  });

  const community = computed(() => {
    const defaultLabel = "XXX社区";
    if (userInfo.value === null) return defaultLabel;
    return userInfo.value.tenant.name || defaultLabel;
  });

  const bloodPerssure = computed(() => {
    const defaultValue = "0/0";
    if (userInfo.value === null) return defaultValue;
    const { bloodPressureSbp, bloodPressureDbp } = userInfo.value;
    return `${bloodPressureSbp}/${bloodPressureDbp}`;
  });

  const healthTarget = computed(() => {
    if (userInfo.value === null) {
      return {
        bmi: "0.00",
        uricAcid: "0.00",
        ldlCholesterol: "0.00",
        fastingBloodGlucose: "0.00",
        bloodPressure: "0/0",
      };
    }
    const {
      bmi,
      uricAcid,
      ldlCholesterol,
      fastingBloodGlucose,
      bloodPressureDbp,
      bloodPressureSbp,
    } = userInfo.value;

    return {
      bmi,
      uricAcid,
      ldlCholesterol,
      fastingBloodGlucose,
      bloodPressure: `${bloodPressureSbp}/${bloodPressureDbp}`,
    };
  });

  const healthPoints = computed(() => {
    const defaultValue = 0;
    if (userInfo.value === null) return defaultValue;
    return userInfo.value.healthPoints || defaultValue;
  });

  const knowledgePoints = computed(() => {
    const defaultValue = 0;
    if (userInfo.value === null) return defaultValue;
    return userInfo.value.knowledgePoints || defaultValue;
  });

  return {
    setUserInfo,
    setNickname,
    setAvatar,
    setPhone,
    setHealthTarget,
    nickname,
    avatar,
    phone,
    community,
    bloodPerssure,
    userInfo,
    healthTarget,
    healthPoints,
    knowledgePoints,
  };
};
